#pragma once

struct lua_State;
class CCharplanDoc;

#define WM_LUA_ERROR WM_USER+200


class CLuaInterface
{
    public:
        CLuaInterface(CCharplanDoc*);
        ~CLuaInterface(void);

        /**
        * (re-)Init()
        * clear and reload lua
        **/
        bool    Init();

        const CStringArray& GetLog();
        bool    IsInDebugMode();

        std::string Eval(const char*);
        void        Execute(const char*);
        bool        Test(const char*);

    public:
        // Lua functions
        static CLuaInterface* GetInterface(lua_State *L);

        static int LuaLog(lua_State *L);
        static int LuaDebug(lua_State *L);
        static int LuaRegisterEvent(lua_State *L);
        static int LuaRegisterAfter(lua_State *L);
        static int LuaUnregister(lua_State *L);

        // Data Triggers
        void OnClassChanged(bool sendevent = true);
        void OnRaceChanged(bool sendevent = true);
        void OnLevelChanged(bool sendevent = true);
        void OnStatsChanged(bool sendevent = true);

        void OnUserOptionChanged(bool sendevent = true);
        void OnSpecChanged(bool sendevent = true);
        void OnRAChanged(bool sendevent = true);
        void OnChampChanged(bool sendevent = true);

    public:
        bool    GetVar(const char* name, int & val, int index = -10002/*LUA_GLOBALSINDEX*/);
        bool    GetVar(const char* name, double & val, int index = -10002/*LUA_GLOBALSINDEX*/);

        int     OpenTable(const char * str, int prealloc=0, int index = -10002/*LUA_GLOBALSINDEX*/);
        void    CloseTable();

    private:
        void    Log(const CString&);
        void    DumpError();

        void    DoAllFiles();
        void    Event(unsigned id, bool execute = true);

        void    SetVar(const char* name, float val, int index = -10002/*LUA_GLOBALSINDEX*/);
        void    SetVar(const char* name, double val, int index = -10002/*LUA_GLOBALSINDEX*/);
        void    SetVar(const char* name, int val, int index = -10002/*LUA_GLOBALSINDEX*/);
        void    SetVar(const char* name, bool val, int index = -10002/*LUA_GLOBALSINDEX*/);
        void    SetVar(const char* name, const char* val, int index = -10002/*LUA_GLOBALSINDEX*/);
        void    SetVar(const char* name, const std::string& val, int index = -10002/*LUA_GLOBALSINDEX*/);

        int     OpenTable(int idx, int prealloc=0, int index = -10002/*LUA_GLOBALSINDEX*/);
        int     CreateTable(const char * str, int prealloc=0, int index = -10002/*LUA_GLOBALSINDEX*/);
        int     CreateTable(int idx, int prealloc=0, int index = -10002/*LUA_GLOBALSINDEX*/);


        void    CallLuaFunction(const char*);

        // Data Triggers
        void    CheckAbilities();
        void    CopyStats();

    private:
        CCharplanDoc*   mp_doc;
        lua_State*      mp_state;

        // Logs
        CStringArray    m_log;

        bool    mb_debug_mode;

        // Events
        std::vector <std::string> m_events_queue;
        std::vector < std::vector <std::string> > m_events;
};

